package com.woniuxy.finance.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.request.ResPage;
import com.woniuxy.finance.entity.Consult;
import com.woniuxy.finance.entity.Search;
import com.woniuxy.finance.mapper.ConsultMapper;
import com.woniuxy.finance.mapper.LogsMapper;
import com.woniuxy.finance.service.ConsultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ConsultServiceImpl implements ConsultService {
    @Resource
    private ConsultMapper consultMapper;
    @Override
    public ResPage<List<Consult>> findAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        //
        List<Consult> ConsultList = consultMapper.findAll();
        // 获取分页信息
        PageInfo<Consult> pageInfo = new PageInfo<>(ConsultList);
        //
        ResPage<List<Consult>> resPage = new ResPage<>();
        resPage.setCurPage(pageInfo.getPageNum());
        resPage.setSize(pageInfo.getSize());
        resPage.setTotal(pageInfo.getTotal());
        resPage.setTotalPage(pageInfo.getPages());
        resPage.setData(pageInfo.getList());
        //
        return resPage;
    }

    @Override
    public  ResPage<List<Consult>> findByCondition(Search search) {
        PageHelper.startPage(search.getPage(), search.getSize());
        List<Consult> goodsList = consultMapper.findByCondition(search);
        return getResPage(search.getPage(),search.getSize(),goodsList);
    }

    @Override
    public Boolean deletePerms(Integer id) {
        return consultMapper.deletePerms(id);
    }

    @Override
    public Boolean addform(Consult consult) {
        return consultMapper.addform(consult);
    }

    @Override
    public Boolean editform(Consult consult) {
        return consultMapper.editform(consult);
    }

    private static ResPage<List<Consult>> getResPage(Integer page, Integer size, List<Consult> goodsList) {
        PageInfo<Consult> pageInfo = new PageInfo<>(goodsList);
        //
        ResPage<List<Consult>> resPage = new ResPage<>();
        resPage.setCurPage(page);
        resPage.setSize(size);
        resPage.setTotal(pageInfo.getTotal());
        resPage.setTotalPage(pageInfo.getPages());
        resPage.setData(goodsList);
        return resPage;
    }
}
